use crate::alloc::boxed::Box;
use crate::global::TIMER_LIST;

pub struct ObjTimer {
    tick: usize,
    func: Box<dyn FnOnce() + Send>,
}

impl ObjTimer {
    pub fn new(t: usize, f: Box<dyn FnOnce() + Send>) -> ObjTimer {
        ObjTimer {
            tick: t + 1,
            func: f,
        }
    }

    pub fn start(self) {
        let obj = Box::new(self);
        let mut timer_list = TIMER_LIST.lock();

        timer_list.push_back(obj)
    }

    pub fn tick(&mut self) -> usize {
        self.tick -= 1;
        self.tick
    }

    pub fn run(self) {
        (self.func)()
    }
}

pub type Timer = ObjTimer;
